unit frmClientList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, GridsEh, DBGridEh, ExtCtrls, SDIMAIN, StdCtrls, ComCtrls,
  ActnList;

type
  TClientList = class(TForm)
    tbu: TPanel;
    DBGridEh1: TDBGridEh;
    DataSource1: TDataSource;
    ADODataSet1: TADODataSet;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Panel1: TPanel;
    RadioGroup1: TRadioGroup;
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    Label1: TLabel;
    ActionList1: TActionList;
    ClientAddNew: TAction;
    ClientEdit: TAction;
    ClientDelete: TAction;
    Button4: TButton;
    Label2: TLabel;
    procedure ShowMain(Sender: TObject);
    procedure ShowSearch;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure DBGridEh1DblClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure DBGridEh1KeyPress(Sender: TObject; var Key: Char);
    procedure RadioGroup1Click(Sender: TObject);
    procedure DateTimePicker1Change(Sender: TObject);
    procedure ClientAddNewExecute(Sender: TObject);
    procedure ClientEditExecute(Sender: TObject);
    procedure ClientDeleteExecute(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    procedure Update;
    { Private declarations }
  public
    SearchProfID: integer;
    SearchMale:integer;
    SearchAgeOt, SearchAgeDo: string;
    SearchPayOt:String;
    { Public declarations }
  end;

var
  ClientList: TClientList;

implementation

uses frmClient, SecretPass;

{$R *.dfm}

procedure TClientList.Button1Click(Sender: TObject);
begin
  ClientForm.AddNew;
  Update;
  SDIAppForm.TotalInfo;  
end;

procedure TClientList.Button2Click(Sender: TObject);
begin
  ClientForm.EditRecord(ADODataSet1['ClientID']);
  Update;
end;

procedure TClientList.Button3Click(Sender: TObject);
var BookMark:TBookmark;
    ID:integer;
    LastRecord:boolean;
begin
  if not SecretPassForm.FormShow then Exit;
  
  ID:=ADODataSet1['ClientID'];
  BookMark:=ADODataSet1.GetBookmark;
  ADODataSet1.Last;
  if ADODataSet1['ClientID']=ID then LastRecord:=true else LastRecord:=false;
  ClientForm.DelRec(ID);
  Update;
  if LastRecord then ADODataSet1.Last else ADODataSet1.GotoBookmark(BookMark);
  ADODataSet1.FreeBookmark(BookMark);
  SDIAppForm.TotalInfo;
end;

procedure TClientList.Button4Click(Sender: TObject);
begin
  Close;
end;

procedure TClientList.ClientAddNewExecute(Sender: TObject);
begin
  Button1Click(Self);
end;

procedure TClientList.ClientDeleteExecute(Sender: TObject);
begin
  Button3Click(Self);
end;

procedure TClientList.ClientEditExecute(Sender: TObject);
begin
  Button2Click(Self);
end;

procedure TClientList.DateTimePicker1Change(Sender: TObject);
begin
  Update;
end;

procedure TClientList.DBGridEh1DblClick(Sender: TObject);
var id:integer;
begin
  id:=ADODataSet1['ClientID'];
  Button2Click(Self);
  ADODataSet1.Locate('ClientID',ID,[loCaseInsensitive]);
end;

procedure TClientList.DBGridEh1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then DBGridEh1DblClick(Self);
end;

procedure TClientList.FormActivate(Sender: TObject);
begin
  DBGridEh1.SetFocus;
end;

procedure TClientList.ShowMain(Sender: TObject);
begin
  SearchProfID:=0;
  SearchMale:=-1;
  SearchAgeOt:='';
  SearchAgeDo:='';
  SearchPayOt:='';
  Button1.Enabled:=true;
  Button3.Enabled:=true;
  Label2.Caption:='';
  ADODataSet1.Close;
  ADODataSet1.CommandText:='SELECT * FROM sp_Prof';
  ADODataSet1.Open;
  ADODataSet1.First;
  ADODataSet1.DisableControls;
  while not ADODataSet1.Eof do
  begin
    DBGridEh1.Columns[1].KeyList.Add(IntToStr(ADODataSet1['ProfID']));
    DBGridEh1.Columns[1].PickList.Add(ADODataSet1['ProfName']);
    ADODataSet1.Next;
  end;
  ClientList.WindowState:=wsMaximized;
  ADODataSet1.EnableControls;
  Update;
  ShowModal;
end;

procedure TClientList.ShowSearch;
begin
  ADODataSet1.Close;
  ADODataSet1.CommandText:='SELECT * FROM sp_Prof';
  ADODataSet1.Open;
  ADODataSet1.First;
  ADODataSet1.DisableControls;
  while not ADODataSet1.Eof do
  begin
    DBGridEh1.Columns[1].KeyList.Add(IntToStr(ADODataSet1['ProfID']));
    DBGridEh1.Columns[1].PickList.Add(ADODataSet1['ProfName']);
    ADODataSet1.Next;
  end;
  ClientList.WindowState:=wsNormal;
  ADODataSet1.EnableControls;
  Update;
  ShowModal;
  RadioGroup1.ItemIndex:=0;
end;

procedure TClientList.RadioGroup1Click(Sender: TObject);
begin
  if RadioGroup1.ItemIndex=1 then
    begin
      DateTimePicker1.Enabled:=true;
      DateTimePicker2.Enabled:=true;
    end
   else
    begin
      DateTimePicker1.Enabled:=false;
      DateTimePicker2.Enabled:=false;
    end;
  Update;
end;

procedure TClientList.Update();
var s, s1:string;
    LastID:integer;
begin
  ADODataSet1.Close;
  s:=  'SELECT ClientID, ClientReg, ClientFIO, ClientWork1, ClientBorn, ClientActual ';
  s:=s+'FROM Clients ';
  case RadioGroup1.ItemIndex of
    0:begin
        s:=s+'WHERE ClientActual>='+FormatDateTime('#mm''/''dd''/''yyyy#',Now()-10);
      end;
    1:begin
        s:=s+'WHERE ClientActual<='+FormatDateTime('#mm''/''dd''/''yyyy#',DateTimePicker2.Date);
        s:=s+' AND ClientActual>='+FormatDateTime('#mm''/''dd''/''yyyy#',DateTimePicker1.Date);
      end;
  else s:=s+'WHERE ClientActual>0';
  end;
  s1:=IntToStr(SearchProfID);
  if SearchProfID<>0 then s:=s+' AND ( ClientWork1='+s1+' OR ClientWork2='+s1+' OR ClientWork3='+s1+
                                  ' OR ClientWork4='+s1+' OR ClientWork5='+s1+' OR ClientWork6='+s1+') ';
  if (SearchMale=0) OR (SearchMale=1) then s:=s+' AND (ClientMale='+IntToStr(SearchMale)+' OR ClientMale=-1)';
  if SearchAgeOt<>'' then s:=s+' AND ClientBorn<'+FormatDateTime('#mm''/''dd''/''yyyy#',Now-StrToInt(SearchAgeOt)*365.4);
  if SearchAgeDo<>'' then s:=s+' AND ClientBorn>'+FormatDateTime('#mm''/''dd''/''yyyy#',Now-StrToInt(SearchAgeDo)*365.4);
  if SearchPayOt<>'' then s:=s+' AND ClientZarplata<='+SearchPayOt;
  s:=s+' ORDER BY ClientFIO ';
  ADODataSet1.CommandText:=s;
  ADODataSet1.Open;
  if ADODataSet1.RecordCount=0 then
    begin
      Button2.Enabled:=false;
      Button3.Enabled:=false;
    end
  else
    begin
      Button2.Enabled:=true;
      Button3.Enabled:=true;
    end;
end;

end.
